package cn.jinjimi.app.main.extend;

import android.content.res.ColorStateList;
import android.util.Log;
import android.view.View;

import cn.jinjimi.app.R;
import cn.jinjimi.app.main.widget.ToolBar.CommonToolbar;
import cn.jinjimi.app.main.widget.ToolBar.ToolbarHelper;
import cn.jinjimi.app.util.PreventContinuousClickUtil;

/**
 * 统一标题栏
 * <p/>
 * Created by Jinsen on 16/1/7.
 */
public abstract class ToolBarCommonActivity extends BaseActivity implements View.OnClickListener {

    private ToolbarHelper mToolbarHelper;
    private CommonToolbar mToolbar;

    @Override
    public void setContentView(int layoutResId) {
        mToolbarHelper = new ToolbarHelper(this, layoutResId);
        mToolbar = mToolbarHelper.createToolbar();
        mToolbar.setOnClickListener(this);
        setContentView(mToolbarHelper.getContentVew());
        setSupportActionBar(mToolbar);
    }

    public View getContentView() {
        return mToolbarHelper != null ? mToolbarHelper.getContentVew() : null;
    }

    public void setRightButtonMargin(int left, int top, int right, int bottom) {
        mToolbar.setRightButtonMargin(left, top, right, bottom);

    }

    /**
     * 仅设置标题文字
     *
     * @param titleId
     */
    public void setOnlyTitle(int titleId) {
        if (mToolbar != null)
            mToolbar.setTitle(titleId);
    }

    /**
     * 设置标题文字
     * (non-Javadoc)
     *
     * @see android.app.Activity#setTitle(CharSequence)
     */
    public void setTitle(CharSequence title) {
        super.setTitle(title);
        if (mToolbar != null)
            mToolbar.baseTitle(title);
    }


    /**
     * 设置标题文字
     * (non-Javadoc)
     *
     * @see android.app.Activity#setTitle(int)
     */
    public void setTitle(int titleId) {
        super.setTitle(titleId);
        setTitle(getText(titleId));
    }

    /**
     * 设置文字类型的action 按钮
     *
     * @param textId
     */
    public void setRightText(int textId) {
        if (mToolbar != null)
            mToolbar.setRightButton(getString(textId));
    }

    /**
     * 设置图标类型的右边按钮
     *
     * @param drawableID
     */
    public void setRightButton(int drawableID) {
        if (mToolbar != null)
            mToolbar.setRightButton(drawableID);
    }

    public void setRightButtonTextColor(ColorStateList csl) {
        if (mToolbar != null)
            mToolbar.setRightButtonTextColor(csl);
    }

    /**
     * 设置文字类型的back 按钮
     *
     * @param textId
     */
    public void setLeftText(int textId) {
        if (mToolbar != null) {
            mToolbar.setLeftButton(getString(textId));
        }
    }

    /**
     * 获取右边按钮
     *
     * @return
     */
    public View getRightBtn() {
        return mToolbar != null ? mToolbar.getRightButton() : null;
    }

    /**
     * 设置右边按钮是否能点击
     *
     * @param enable
     */
    public void setRightBtnEnable(boolean enable) {

        if (mToolbar != null)
            mToolbar.setRightBtnEnable(enable);

        Log.e("rightButtonEnable", enable + "");
    }

    /**
     * 设置右边按钮显示隐藏
     */
    public void setRightBtnVisibility(boolean hasShow) {
        if (mToolbar != null)
            mToolbar.showRightButton(hasShow);
    }

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
            getSupportFragmentManager().popBackStack();
        } else {
            if (!isTaskRoot())
                super.onBackPressed();
        }
    }

    @Override
    public void onClick(View v) {
        if (PreventContinuousClickUtil.isContinuousClick())
            return;
        switch (v.getId()) {
            case R.id.btn_left:
                leftAction();
                break;
            case R.id.btn_right:
                rightAction();
                break;
            case R.id.tv_title_bar_title:
                titleAction();
                break;
        }
    }

    public void hideToolBar() {
        if (mToolbarHelper != null) {
            mToolbarHelper.hideToolBar();
        }
    }

    public void showToolBar() {
        if (mToolbarHelper != null) {
            mToolbarHelper.showToolBar();
        }
    }

    /* title按钮的点击事件处理 */
    protected void titleAction() {
    }

    /* 左边按钮的点击事件处理 */
    protected void leftAction() {
        onBackPressed();
    }

    /* 右边按钮的点击事件处理 */
    protected void rightAction() {
    }
}
